#!/bin/bash
#初始化二级跳板机

error_exit(){
	echo "ERROR:初始化跳板机失败, $1" >> ${logFile}
	echo "ERROR:初始化跳板机失败, $1"
	exit 1
}

sshIp=$1
sshUser=$2
sshPass=$3
sshRootPass=$4
port=$5
rootPath=$6
logFile=$7

sshNewGroup="beetle"
sshNewUser="beetle"


#一级跳板机参数
jumpSerIp=$8
ssh_login_user=$9
ssh_key_file=${10}
ssh_port=${11}

shellPath=/home/${ssh_login_user}/shell

#sshIp=192.168.56.135
#sshUser=wangjian
#sshPass=1
#sshRootPass=1
#port=22
#rootPath=/usr/local/Beetle
#logFile=/home/beetle/a.log
#shellPath=/home/beetle/shell
#sshNewGroup="beetle"
#sshNewUser="beetle"
#
#
##一级跳板机参数
#jumpSerIp=192.168.56.134
#ssh_login_user=beetle
#ssh_key_file=.Beetle/common_id_rsa
#ssh_port=22


#当前beetle机器登录用户名
loginUser=`whoami`
ssh_key_file=/home/${loginUser}/${ssh_key_file}

echo "==========初始化跳板机开始==========" >> ${logFile}

if [ ! ${rootPath} ]
then
    error_exit "参数存在空值rootPath=${rootPath}"
fi

#拷贝脚本文件
echo "==========拷贝文件beetle_terminal_user_create.sh开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "expect ${shellPath}/beetle_terminal_scp.sh ${port} ${sshIp} ${sshUser} ${sshPass} ${shellPath}/beetle_terminal_user_create.sh /home/${sshUser}/ 2>&1" >> ${logFile} || error_exit "拷贝文件beetle_terminal_user_create.sh失败"
echo "==========拷贝文件beetle_terminal_user_create.sh结束==========" >> ${logFile}

echo "==========拷贝文件id_rsa开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "expect ${shellPath}/beetle_terminal_scp.sh ${port} ${sshIp} ${sshUser} ${sshPass} /home/beetle/.Beetle/common_id_rsa /home/${sshUser}/id_rsa" >> ${logFile} || error_exit "拷贝文件common_id_rsa失败"
echo "==========拷贝文件id_rsa结束==========" >> ${logFile}

echo "==========拷贝文件id_rsa.pub开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "expect ${shellPath}/beetle_terminal_scp.sh ${port} ${sshIp} ${sshUser} ${sshPass} /home/beetle/.Beetle/id_rsa.pub /home/${sshUser}/" >> ${logFile} || error_exit "拷贝文件id_rsa.pub失败"
echo "==========拷贝文件id_rsa.pub结束==========" >> ${logFile}

echo "==========初始化用户${sshNewUser}开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "expect ${shellPath}/beetle_jumpserver_user_init.sh ${port} ${sshIp} ${sshUser} ${sshPass} ${sshRootPass} ${sshNewUser} ${sshNewGroup} ${rootPath}" >> ${logFile} || error_exit "远程机创建用户失败"
echo "==========初始化用户${sshNewUser}结束==========" >> ${logFile}

echo "==========校验ssh免密登录开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "expect ${shellPath}/beetle_validate_ssh.sh ${sshIp} ${sshNewUser} ${loginUser} ${port}" >> ${logFile} || error_exit "校验ssh免密登录失败"
echo "==========校验ssh免密登录结束==========" >> ${logFile}

echo "==========拷贝文件beetle_validate_ssh.sh开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "scp -P ${port} -i /home/${loginUser}/.Beetle/common_id_rsa ${shellPath}/beetle_validate_ssh.sh ${sshNewUser}@${sshIp}:/home/${sshNewUser}/shell/" >> ${logFile} || error_exit "拷贝文件beetle_validate_ssh.sh失败"
echo "==========拷贝文件beetle_validate_ssh.sh结束==========" >> ${logFile}

echo "==========拷贝文件beetle_jumpserver_init.sh开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "scp -P ${port} -i /home/${loginUser}/.Beetle/common_id_rsa ${shellPath}/beetle_jumpserver_init.sh ${sshNewUser}@${sshIp}:/home/${sshNewUser}/shell/" >> ${logFile} || error_exit "拷贝文件beetle_jumpserver_init.sh失败"
echo "==========拷贝文件beetle_jumpserver_init.sh结束==========" >> ${logFile}

echo "==========拷贝文件beetle_terminal_scp.sh开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "scp -P ${port} -i /home/${loginUser}/.Beetle/common_id_rsa ${shellPath}/beetle_terminal_scp.sh ${sshNewUser}@${sshIp}:/home/${sshNewUser}/shell/" >> ${logFile} || error_exit "拷贝文件beetle_terminal_scp.sh失败"
echo "==========拷贝文件beetle_terminal_scp.sh结束==========" >> ${logFile}

echo "==========拷贝文件beetle_jumpserver_user_init.sh开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "scp -P ${port} -i /home/${loginUser}/.Beetle/common_id_rsa ${shellPath}/beetle_jumpserver_user_init.sh ${sshNewUser}@${sshIp}:/home/${sshNewUser}/shell/" >> ${logFile} || error_exit "拷贝文件beetle_jumpserver_user_init.sh失败"
echo "==========拷贝文件beetle_jumpserver_user_init.sh结束==========" >> ${logFile}

echo "==========拷贝文件tomcat.sh开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "scp -P ${port} -i /home/${loginUser}/.Beetle/common_id_rsa ${shellPath}/tomcat.sh ${sshNewUser}@${sshIp}:/home/${sshNewUser}/shell/" >> ${logFile} || error_exit "拷贝文件tomcat.sh失败"
echo "==========拷贝文件tomcat.sh结束==========" >> ${logFile}

echo "==========拷贝文件beetle_release_tomcat_log.sh开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "scp -P ${port} -i /home/${loginUser}/.Beetle/common_id_rsa ${shellPath}/beetle_release_tomcat_log.sh ${sshNewUser}@${sshIp}:/home/${sshNewUser}/shell/" >> ${logFile} || error_exit "拷贝文件beetle_release_tomcat_log.sh失败"
echo "==========拷贝文件beetle_release_tomcat_log.sh结束==========" >> ${logFile}

echo "==========拷贝文件beetle_php_bak.sh开始==========" >> ${logFile}
ssh ${ssh_login_user}@${jumpSerIp} -p ${ssh_port} -i ${ssh_key_file} "scp -P ${port} -i /home/${loginUser}/.Beetle/common_id_rsa ${shellPath}/beetle_php_bak.sh ${sshNewUser}@${sshIp}:/home/${sshNewUser}/shell/" >> ${logFile} || error_exit "拷贝文件beetle_php_bak.sh失败"
echo "==========拷贝文件beetle_php_bak.sh结束==========" >> ${logFile}

echo "==========初始化跳板机成功==========" >> ${logFile}
echo "success"